package com.example.black.myapplication.dragswip;

import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import com.example.black.myapplication.R;

/**
 * 触摸帮助类
 */
public class MyItemTouchHelper extends ItemTouchHelper.Callback {
    private ItemMoveListener moveListener;

    public MyItemTouchHelper(ItemMoveListener moveListener) {
        this.moveListener = moveListener;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {

        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;//允许上下滑动
        int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;//允许左右滑动
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        return viewHolder.getItemViewType() == target.getItemViewType()
                && moveListener != null
                && moveListener.onMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        if (moveListener != null) {
            moveListener.onSwipedHorizontal(viewHolder.getAdapterPosition());
        }
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            viewHolder.itemView.setBackgroundColor(viewHolder.itemView.getContext().getResources().getColor(R.color.colorAccent));
        }
        super.onSelectedChanged(viewHolder, actionState);
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        viewHolder.itemView.setBackgroundColor(viewHolder.itemView.getContext().getResources().getColor(R.color.white));
        viewHolder.itemView.setAlpha(1);
        viewHolder.itemView.setScaleX(1);
        viewHolder.itemView.setScaleY(1);
        super.clearView(recyclerView, viewHolder);
    }

    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
//            float alpha = 1 - Math.abs(dX) / viewHolder.itemView.getWidth();
//            viewHolder.itemView.setAlpha(alpha);
//            viewHolder.itemView.setScaleY(alpha);
        }
        if(Math.abs(dX) <= (viewHolder.itemView.getWidth()/2)){//为啥要绝对值
            viewHolder.itemView.setTranslationX((float) (-0.5 * viewHolder.itemView.getWidth()));//正负怎么看
        }else {
            viewHolder.itemView.setTranslationX(dX);//正负怎么看
        }
        //自动处理了平移setTranslationX方法
      //  super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }

    interface ItemMoveListener {
        /**
         *
         * @param srcPosition  源位置
         * @param targetPosition 目标位置
         * @return 移动源
         */
        boolean onMove(int srcPosition, int targetPosition);

        /**
         *
         * @param position 对应位置
         */
        void onSwipedHorizontal(int position);
    }
}
